package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.OrderHistoryAdapter;
import com.example.smartcity.ui.Bean.OrderHistoryBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class OrderHistory extends AppCompatActivity {

    private RecyclerView RV_orderHistory;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_history);
        RV_orderHistory = findViewById(R.id.RV_orderHistory);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/order/list?pageNum=1&pageSize=100&userId="+getSharedPreferences("selfMessage",MODE_PRIVATE).getString("userID",null);
        OkHttpUtils.getCall(newUrl,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                OrderHistoryBean bean = gson.fromJson(response.body().string(),OrderHistoryBean.class);
                if (bean.getCode()==200){
                    List<OrderHistoryBean.RowsBean> list = bean.getRows();
                    RV_orderHistory.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayoutManager manager = new LinearLayoutManager(OrderHistory.this);
                            manager.setOrientation(RecyclerView.VERTICAL);
                            RV_orderHistory.setLayoutManager(manager);
                            RV_orderHistory.setAdapter(new OrderHistoryAdapter(list,R.layout.orderhistoryitemlayout));
                        }
                    });

                }else{
                    Log.d("TAG", "onResponse: 请求失败");
                }
            }
        });
    }
}